package cn.cdyx.common.utils;

import cn.cdyx.common.pojo.TBSYSTABLES;
import cn.cdyx.common.cache.TbsysTablesCache;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author Watermelon
 * @Date 2023-01-12 19:54
 * 取表工具类
 */
@Slf4j
@Component
public class TableNameUtil {
    @Autowired
    private TbsysTablesCache tbsysTablesCache;

    private static TableNameUtil tableNameUtils;

    @PostConstruct
    public void init(){
        tableNameUtils = this;
        tableNameUtils.tbsysTablesCache = this.tbsysTablesCache;
    }
    /**
     * Watermelon
     * 取单表
     * 只处理了部分表需要的自己再补充
     *
     * @param tablePrefix
     * @param param
     * @return
     */
    public static String getTableName(String tablePrefix, String param) {
        //TBSYSTABLES tbsystables
        String tableName = "";
        TBSYSTABLES tbsystable = getTbsystables(tablePrefix);
        switch (tbsystable.getItype()) {
            case 0://单表
                tableName = tbsystable.getCdatabase() + ".." + tablePrefix ;
                break;
            case 10://病区表
                if (tablePrefix.substring(tablePrefix.length() - 2).equals("BQ")) {
                    tableName = tbsystable.getCdatabase() + ".." + tablePrefix + param;
                } else {
                    tableName = tbsystable.getCdatabase() + ".." + tablePrefix + "BQ" + param;
                }
                break;
            default:
                log.info("暂不支持该类型！请自行在getTableName中实现");
                tableName = "";
        }
        return tableName;
    }

    /**
     * Watermelon
     * 取年月表单表
     * @param tablePrefix
     * @param BbeginDateTime
     * @return
     */
    public static @NotNull String getYearTableName(String tablePrefix, Date BbeginDateTime){
        String tableName = "" ;
        TBSYSTABLES tbsystable = getTbsystables(tablePrefix);
        String Year = new SimpleDateFormat("yyyy").format(BbeginDateTime);
        String Monuth = new SimpleDateFormat("yyyyMM").format(BbeginDateTime);
        switch (tbsystable.getItype()) {
            case 4://年库月表
                tableName = tbsystable.getCdatabase()+Year+".."+tablePrefix+Monuth;
                break;
            case 5://年库年表
                tableName = tbsystable.getCdatabase()+Year+".."+tablePrefix+Year;
                break;
            default:
                tableName = "" ;
                log.info("暂不支持该类型！请自行在getYearTableName中实现");
        }
        return tableName;
    }
    /**
     * Watermelon
     * 取年表集合
     *
     * @param tablePrefix
     * @param beginTime
     * @param endTime
     * @return
     */
    public static @NotNull List<String> getYearTableNames(String tablePrefix, Date beginTime, Date endTime) {
        List<String> tableList = new ArrayList<String>();
        TBSYSTABLES tbsystable = getTbsystables(tablePrefix);
        int beginYear = Integer.parseInt(new SimpleDateFormat("yyyy").format(beginTime));
        int endYear = Integer.parseInt(new SimpleDateFormat("yyyy").format(endTime));
        Date aDate = new Date();
        switch (tbsystable.getItype()) {
            case 4://年库月表
                for(int i = beginYear; i <= endYear; i++){
                    for(int j = 1; j <= 12; j++){
                        try {
                            aDate = new SimpleDateFormat("yyyyMM").parse(Integer.toString(i)+String.format("%02d", j));
                        } catch (ParseException e) {
                            throw new RuntimeException(e);
                        }
                        if(aDate.compareTo(beginTime)>=0 && endTime.compareTo(aDate)>=0) {
                            tableList.add(tbsystable.getCdatabase() + Integer.toString(i) + ".." + tablePrefix + Integer.toString(i)+String.format("%02d", j));
                        }
                    }
                }
            case 5://年库年表
                for(int i = beginYear; i <= endYear; i++){
                    tableList.add(tbsystable.getCdatabase()+Integer.toString(i)+".."+tablePrefix+Integer.toString(i));
                }
            default:
        }
        return tableList;
    }

    private static @Nullable TBSYSTABLES getTbsystables(String cmc) {
        TBSYSTABLES tbsystable = null;
        for (TBSYSTABLES tbsystables : tableNameUtils.tbsysTablesCache.tbsystables) {
            if (Objects.equals(tbsystables.getCmc(),cmc)) {
                tbsystable = tbsystables;
                break;
            }
        }
        if (tbsystable == null) {
            log.info("未查询到[" + cmc + "]表对象！");
            return null;
        } else {
            return tbsystable;
        }
    }
}
